﻿
using System.IO;
namespace AI3SaveEditor.AI3Files
{
    class Js3fsiFile : ClothesFile
    {
        public Js3fsiFile(string fileName)
            : base(fileName) {
            Load();
        }

        public override void ChangeFileName() {

            string dir = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(this.FileName)), this.Name);
            Directory.Move(Path.GetDirectoryName(this.FileName), dir);
            this.file = new FileInfo(Path.Combine(dir, Path.GetFileName(this.FileName)));

            if (File.Exists(this.FileName)) {
                string dest = Path.Combine(Path.GetDirectoryName(this.FileName), this.Name + ".js3fsi");
                File.Move(this.FileName, dest);
            }

            string sourceFile = Path.Combine(Path.GetDirectoryName(this.FileName), Path.GetFileNameWithoutExtension(this.FileName) + ".pp");
            if (File.Exists(sourceFile)) {
                DecryptUtil.DecryptFile(sourceFile);
                string oldDir = Path.Combine(Path.GetDirectoryName(sourceFile), Path.GetFileNameWithoutExtension(this.FileName));
                string newDir = Path.Combine(Path.GetDirectoryName(sourceFile), this.Name);
                Directory.Move(oldDir, newDir);


                foreach (var file in Directory.GetFiles(newDir)) {
                    int oldFileLength = Path.GetFileNameWithoutExtension(this.FileName).Length;
                    string newFile = Path.Combine(newDir, this.Name + Path.GetFileName(file).Substring(oldFileLength));
                    File.Move(file, newFile);
                }
                DecryptUtil.DecryptDir(newDir);
            }
        }
    }
}
